import SymbolList from "./SymbolList"
import browserUtil from "./utils/browser.util"
import Engine from "./Engine"
import Showcase from "./Showcase"
import BundleConnector from "./BundleConnector"
import BundleMessengerFactoryFetcher from "./BundleMessengerFactoryFetcher"
import ConnectSdk from "./ConnectSdk"
import Sdk from "./module/SdkModule"
declare global {
  interface Window {
    MP_SDK: BundleSdkBootstrap
  }
}
class BundleSdkBootstrap {
  engine: Engine
  constructor(engine: Engine) {
    this.engine = engine
  }
  async connect(options = {}) {
    const sdk = await this.engine.getModuleBySymbol<Sdk>(SymbolList.SDK)

    return ConnectSdk.connect(new BundleConnector(sdk, options), new BundleMessengerFactoryFetcher())
  }
}
//读取配置
window.addEventListener(
  "app-start",
  (e: any) => {
    const config = e.detail.config || {}
    const engine = new Engine()
    const showcase = new Showcase(false, config)
    engine.loadApplication(showcase, showcase.setError.bind(showcase)), browserUtil.canTouch() || document.body.classList.add("no-touch")
    window.MP_SDK = new BundleSdkBootstrap(engine)
    if (process.env.NODE_ENV === "development") {
      window["engine"] = engine
    }
  },
  true
)
//启动
;(function () {
  var detailObject = {
    config: {
      apiHost: process.env.API_ENV
    }
  }

  window.dispatchEvent(new CustomEvent("app-start", { detail: detailObject }))
})()
